package com.example.mysql.user.web;

import com.example.mysql.user.entity.User;
import com.example.mysql.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 内置 CRUD 演示
 */
@Slf4j
@Api("mysql集群基本操作")
@RestController
public class UserController {
    @Resource
    private IUserService userService;

    @ApiOperation("保存")
    @PostMapping("/save")
    public void save(@RequestBody User user) {
        log.info("开始执行insert方法，id={}", user.getId());
        userService.insert(user);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public void update(@RequestBody User user) {
        log.info("开始执行updateById方法，id={}", user.getId());
        userService.updateById(user);
    }

    @ApiOperation("删除")
    @GetMapping("/delete")
    public void delete(@RequestParam("id") Integer id) {
        log.info("开始执行deleteById方法，id={}", id);
        int i = userService.deleteById(id);
        log.info("删除成功：", i);
    }

    @ApiOperation("查询")
    @GetMapping("/selectById")
    public User selectById(@RequestParam("id") Integer id) {
        log.info("开始执行selectById方法，id={}", id);
        User user = userService.selectById(id);
        log.info("数据为=={}", user);
        return user;
    }


    @ApiOperation("查询")
    @GetMapping("/selectList")
    public List<User> selectList() {
        log.info("开始执行selectList方法");
        List<User> list = userService.selectList();
        log.info("查询到的数据为，list={}", list);
        return list;
    }

}
